﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dsgridview.aspx.cs" Inherits="WebNull.dsgridview" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        #form1 {
            width: 671px;
        }
    </style>

    <script type="text/javascript">
        function GetAllCheckBox(CheckAll) {
            var items = document.getElementsByTagName("input");
            for (var i = 0; i < items.length; i++) {
                if (items[i].type == "checkbox") {
                    items[i].checked = CheckAll.checked;
                }
            }
        }

        function GetSelectCheckBox() {
            var items = document.getElementsByTagName("input");
            for (var i = 0; i < items.length; i++) {
                if (items[i].type == "checkbox") {
                    if (items[i].checked )
                    {
                        return confirm("您确定要删除吗?");
                    }
                }
            }
            alert("至少选择一项");
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" Height="338px" Style="margin-right: 115px" Width="454px"
             DataKeyNames="id" AllowPaging="True" PageSize="3" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>

                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="catid" HeaderText="catid" SortExpression="catid" />
                <asp:TemplateField HeaderText="catname" SortExpression="catname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("catname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("catname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="catParent" HeaderText="catParent" SortExpression="catParent" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:TemplateField HeaderText="选择">
                    <HeaderTemplate>
                        <input id="Checkbox2" type="checkbox" onclick="GetAllCheckBox(this)" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <input id="Checkbox3" type="checkbox" value='<%# Eval("id")%>'   name="userchk" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                none
            </EmptyDataTemplate>
            <PagerTemplate>
                当前第:
               <%-- //((GridView)Container.NamingContainer)就是为了得到当前的控件--%>
                <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                页/共:
                <%--//得到分页页面的总数--%>
                <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
                页
                 <%--//如果该分页是首分页，那么该连接就不会显示了.同时对应了自带识别的命令参数CommandArgument--%>
                <br />
                <br />
                <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                    Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
                &nbsp;
                <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                    CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
                <%--//这里将CommandArgument即使点击该按钮e.newIndex 值为3--%> 
                &nbsp; 转到 第
                <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
                <%--//如果该分页是尾页，那么该连接就不会显示了--%>
                <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
                    CommandName="Page" Text="GO" />
                &nbsp;&nbsp;
                <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                    Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
                &nbsp;
                <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                    Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
            </PagerTemplate>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdataConnectionString %>"
            SelectCommand="SELECT top 3 * FROM [catproduct] where id not in (select top @n * from  [catproduct] )" UpdateCommand="UPDATE catproduct SET catname = @catname WHERE (id = @id)" DeleteCommand="DELETE FROM catproduct WHERE (id = @id)">
            <DeleteParameters>
                <asp:Parameter Name="id" />
            </DeleteParameters>
            <SelectParameters>
                <asp:Parameter Name="n" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="catname" />
                <asp:Parameter Name="id" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" OnClientClick="javascript:GetSelectCheckBox()" OnClick="Button1_Click" Text="Button" />
    </form>
</body>
</html>
